package com.easyweb.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easyweb.common.Constants;
import com.easyweb.common.PrintUtils;
import com.easyweb.common.SystemUtils;
import com.easyweb.model.DiskInfo;
import com.easyweb.model.MemoryInfo;
import com.easyweb.model.ResultMsg;
import com.easyweb.model.SystemInfo;
import com.easyweb.model.Users;

@Controller
@RequestMapping("/monit")
public class MonitorController {

	@RequestMapping(value="/sys",method=RequestMethod.GET)
	public void getSystemInfo(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		SystemInfo system = SystemUtils.getSystemInfo();
		try {
			List<DiskInfo> disk = SystemUtils.getDiskInfo(new Sigar());
			system.setDisk(disk);
		} catch (SigarException e) {
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, "get disk fail:"+e.getMessage()));
			return;
		}
		try {
			MemoryInfo memory = SystemUtils.getMemoryInfo(new Sigar());
			system.setMemory(memory);
		} catch (SigarException e) {
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, "get memory fail:"+e.getMessage()));
			return;
		}
		PrintUtils.printMsg(response, system);
	}
}
